import 'dart:typed_data';
import 'package:isolate/ports.dart';

import 'package:ffi/ffi.dart';
import 'dart:convert';
import 'dart:ffi';
import '../ffi/TestMod.dart';

///
/// @ProjectName:    flutter_cpp_sdk
/// @Package:        sdk
/// @ClassName:      TestApp
/// @Description:     描述
/// @Author:         wujehy
/// @Time:     21-2-17 下午9:27
///
class RequestDataType {
  bool status;
  int taskid;
  int type;
  String token;
  Uint8List data;
}

class ResponseDataType {
  int code;
  bool status;
  int taskid;
  int type;
  String token;
  Uint8List data;

  ResponseDataType(
      {this.code, this.status, this.taskid, this.type, this.token, this.data});

  factory ResponseDataType.from_response(List ffiResp) {
    ResponseDataType responseDataType = new ResponseDataType();
    responseDataType.code = ffiResp[1];
    responseDataType.taskid = ffiResp[3];
    responseDataType.type = ffiResp[4];
    responseDataType.token = ffiResp[5];
    responseDataType.data = ffiResp[6];
    return responseDataType;
  }
}

class TestStringCallbackData extends BaseCallBackData {
  TestStringCallbackData(int code, String errmsg) : super(code, errmsg);
  String dataStr;
}

class ResponseCallbackData extends BaseCallBackData {
  ResponseCallbackData(int code, String errmsg) : super(code, errmsg);
  ResponseDataType response;
}

class TestApp {
  static Future<TestStringCallbackData> Sync([int time = 3]) {
    final future = singleResponseFuture<List>(
        (port) => nativeTestFunc_Sync(port.nativePort, time));
    return future.then((List response) {
      print(" callback success respose = $response ");
      //　使用json 解析
      TestStringCallbackData callBackData =
          new TestStringCallbackData(response[0], response[1] ?? "");
      String dataStr = utf8.decode(response[2]);
      callBackData.dataStr = dataStr;
      return callBackData;
    }).catchError((err) {
      print(err);
    });
  }

  static Future<TestStringCallbackData> Async([int time = 3]) {
    final future = singleResponseFuture<List>(
        (port) => nativeTestFunc_Async(port.nativePort, time));
    return future.then((List response) {
      print(" callback success respose = $response ");
      //　使用json 解析
      TestStringCallbackData callBackData =
          new TestStringCallbackData(response[0], response[1] ?? "");
      String dataStr = utf8.decode(response[2]);
      callBackData.dataStr = dataStr;
      return callBackData;
    }).catchError((err) {
      print(err);
    });
  }

  static Future<ResponseCallbackData> CustomizeRequest(
      RequestDataType request) {
    String testMsg = "test";
    final Pointer<Uint8> result =
        allocate<Uint8>(count: request.data.length + 1);
    final Uint8List nativeString = result.asTypedList(request.data.length + 1);
    nativeString.setAll(0, request.data);
    nativeString[request.data.length] = 0;
    final future = singleResponseFuture<List>((port) =>
        nativeTestFunc_Customize(
            port.nativePort,
            200,
            Utf8.toUtf8(testMsg),
            request.status ? 1 : 0,
            request.taskid,
            Utf8.toUtf8(request.token),
            result.cast(),
            request.data.length));
    return future.then((List response) {
      print(" callback success respose = $response ");
      //　使用json 解析
      if (response[0] != 1) {
        print("不符合协议规范");
        ResponseCallbackData callBackData =
            new ResponseCallbackData(500, "不符合协议规范");
        return callBackData;
      } else {
        ResponseCallbackData callBackData =
            new ResponseCallbackData(response[1], response[2]);
        callBackData.response = ResponseDataType.from_response(response);
        return callBackData;
      }
    }).catchError((err) {
      print(err);
    });
  }
}
